/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.engine.gui;

import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.builder.ElementBuilder.ChildLayoutType;
import de.lessvoid.nifty.builder.ImageBuilder;
import de.lessvoid.nifty.builder.PanelBuilder;
import de.lessvoid.nifty.render.NiftyImage;
import de.lessvoid.nifty.render.image.ImageModeFactory;
import de.lessvoid.nifty.screen.Screen;
import etherblocks.engine.states.*;
import etherblocks.texturepacks.*;
import com.cubes.*;

/**
 *
 * @author Carl
 */
public class ScreenController_IngameMenu extends GameScreenController{

    public ScreenController_IngameMenu(){
        
    }

    @Override
    protected void initialize(){
        Screen screen = nifty.getScreen("start");
        Element itemBar = screen.findElementByName("itemBar");
        float panelX = 20.75f;
        int tileX = 0;
        int tileY = 0;
        for(int i=0;i<8;i++){
            PanelBuilder panelBuilder = new PanelBuilder("itemContainer_" + i);
            panelBuilder.x(panelX + "%");
            panelBuilder.y("84.85%");
            panelBuilder.width("6%");
            panelBuilder.height("8.2%");
            panelBuilder.childLayout(ChildLayoutType.Center);
            ImageBuilder imageBuilder = new ImageBuilder("itemImage_" + i);
            imageBuilder.width("100%");
            imageBuilder.height("100%");
            panelBuilder.image(imageBuilder);
            panelBuilder.image(new ImageBuilder("itemOverlay_Selected_" + i){{
                width("100%");
                height("100%");
                filename("Interface/images/item_selected.png");
            }});
            panelBuilder.build(nifty, screen, itemBar);
            panelX += 7.5f;
            tileX++;
            if(tileX > 15){
                tileX = 0;
                tileY++;
            }
        }
        selectItem(0);
    }
    
    public void selectItem(int index){
        for(int i=0;i<8;i++){
            Element item = getElementByID("itemOverlay_Selected_" + i);
            if(item != null){
                item.setVisible(i == index);
            }
        }
    }
    
    public void setItem(int index, BlockType blockType){
        TexturePack texturePack = mainApplication.getStateManager().getState(TexturePackAppState.class).getTexturePack();
        String terrainFilePath = texturePack.getTerrainTexturePath();
        int textureTileSize = (terrainFilePath.equals("Textures/terrain.png")?16:32);
        NiftyImage image = createImage(terrainFilePath);
        BlockSkin_TextureLocation textureLocation = getBlockSkin_ExampleTextureLocation(blockType);
        image.setImageMode(ImageModeFactory.getSharedInstance().createImageMode("subimage:" + (textureLocation.getColumn() * textureTileSize) + "," + (textureLocation.getRow() * textureTileSize) + "," + textureTileSize + "," + textureTileSize, "resize"));
        getImageRenderer("itemImage_" + index).setImage(image);
    }
    
    private BlockSkin_TextureLocation getBlockSkin_ExampleTextureLocation(BlockType blockType){
        BlockTerrainControl blockTerrain = new BlockTerrainControl(new CubesSettings(mainApplication), new Vector3Int(1, 1, 1));
        blockTerrain.setBlock(new Vector3Int(0, 0, 0), BlockManager.getClass(blockType.getType()));
        return blockType.getSkin().getTextureLocation(blockTerrain.getChunks()[0][0][0], new Vector3Int(0, 0, 0), Block.Face.Front);
    }
    
    public void setBlockToolsVisible(boolean isVisible){
        setItemBarVisible(isVisible);
        setCrosshairVisible(isVisible);
    }
    
    public void setItemBarVisible(boolean isVisible){
        getElementByID("itemBar").setVisible(isVisible);
    }
    
    public void setCrosshairVisible(boolean isVisible){
        getElementByID("crosshair").setVisible(isVisible);
    }
}
